//
//  WmcRecognitionSettingsListCell.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/10/18.
//

import UIKit

class WmcRecognitionSettingsListCell: WmcBaseTableCell {
    // Internal

    var switchClosure: (((isOn: Bool, indexPath: IndexPath)) -> Void)?
    
    var singleClickClosure: (((optionIndex: Int, indexPath: IndexPath)) -> Void)?

    override func makeUI() {
        contentView.addSubview(switchBtn)
        contentView.addSubview(nameLabel)
        contentView.addSubview(contentLabel)
        contentView.addSubview(timeLabel)
        contentView.addSubview(arrowView)
        contentView.addSubview(singleOptionView)
        contentView.addSubview(line)

        switchBtn.snp.makeConstraints { make in
            make.width.equalTo(50)
            make.height.equalTo(26)
            make.left.equalTo(16)
            make.centerY.equalToSuperview()
        }

        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(switchBtn.snp.right).offset(8)
            make.centerY.equalTo(switchBtn.snp.centerY)
            make.width.equalTo(70)
        }

        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.right).offset(10)
            make.right.equalTo(arrowView.snp.left).offset(-4)
            make.centerY.equalTo(switchBtn.snp.centerY)
        }

        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.right).offset(10)
            make.right.equalTo(arrowView.snp.left).offset(-4)
            make.centerY.equalTo(switchBtn.snp.centerY)
        }

        arrowView.snp.makeConstraints { make in
            make.right.equalTo(-16)
            make.centerY.equalTo(switchBtn.snp.centerY)
            make.width.height.equalTo(16)
        }

        line.snp.makeConstraints { make in
            make.left.equalTo(74)
            make.right.equalTo(-16)
            make.height.equalTo(1)
            make.bottom.equalTo(0)
        }

        singleOptionView.snp.makeConstraints { make in
            make.right.equalTo(arrowView.snp.right)
            make.centerY.equalTo(switchBtn.snp.centerY)
            make.height.equalTo(40)
            make.width.equalTo(0)
        }
    }

    override func bindEvent() {
        singleOptionView.selectedIndexClosure = { [weak self] idx in
            guard let weakSelf = self, let indexPath = weakSelf.indexPath else { return }
            let data = (optionIndex: idx, indexPath: indexPath)
            weakSelf.singleClickClosure?(data)
        }
    }

    func bindModel(_ model: WmcWatermarkOptionModel, indexPath: IndexPath) {
        self.model = model
        self.indexPath = indexPath

        switchBtn.isOn = model.isOpen
        switchBtn.isEnabled = model.isAction
        if model.title.count > 4 {
            nameLabel.text = model.title.subString(to: 4)
        } else {
            nameLabel.text = model.title
        }
        
        if model.content.isEmpty {
            contentLabel.text = "点击编辑"
        } else {
            var contentText = model.content
            if model.fieldType == .results {
                contentText = "\(model.content)根"
            }
            contentLabel.text = contentText
        }
        
        if model.fieldType == .time {
            timeLabel.textColor = model.isOpen ? .wmc_gray_444649 : .wmc_grayH13_ACAEAF
        } else {
            if model.isOpen, !model.content.isEmpty {
                contentLabel.textColor = .wmc_gray_444649
            } else {
                contentLabel.textColor = .wmc_grayH13_ACAEAF
            }
        }
        
        arrowView.isHidden = !model.enableEditContent

        if model.fieldType == .single {
            singleOptionView.isHidden = false
            contentLabel.isHidden = true
            timeLabel.isHidden = true

            let selectedIndex = model.options.firstIndex(of: model.content) ?? 0
            singleOptionView.updateData(model.options, selectedIndex: selectedIndex, isEnable: model.isOpen)

            if model.options.count > 0 {
                let maxWidth = Int(UIScreen.main.bounds.width - 180)
                var width = 64 * model.options.count + 8 * (model.options.count - 1) + 4
                if width > maxWidth {
                    width = maxWidth
                }
                singleOptionView.snp.updateConstraints { make in
                    make.width.equalTo(width)
                }
            }
        } else {
            singleOptionView.isHidden = true
            contentLabel.isHidden = model.fieldType == .time
            timeLabel.isHidden = model.fieldType != .time
        }

        let arrowWidth = model.enableEditContent ? 16 : 0
        arrowView.snp.updateConstraints { make in
            make.width.equalTo(arrowWidth)
        }
        arrowView.isHidden = !model.enableEditContent
    }

    // Private

    private var model: WmcWatermarkOptionModel?

    private var indexPath: IndexPath?
    private lazy var switchBtn: UISwitch = {
        let view = UISwitch()
        view.onTintColor = .wmc_blue_5290FD
        view.tintColor = .wmc_green_E6F8E6
        view.addTarget(self, action: #selector(switchDidChange(sender:)), for: .valueChanged)
        return view
    }()

    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.textColor = .wmc_gray_444649
        label.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        return label
    }()

    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.textColor = .wmc_gray_444649
        label.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        label.textAlignment = .right
        return label
    }()

    private lazy var timeLabel: WmcDateLabel = {
        let label = WmcDateLabel.YP.Create(font: UIFont.systemFont(ofSize: 14, weight: .medium), color: UIColor.wmc_gray_444649, alignment: .right)
        label.isHidden = true
        return label
    }()

    private lazy var arrowView: UIImageView = {
        let view = UIImageView()
        view.image = "recognition_arrow".wmc_image
        return view
    }()

    private lazy var singleOptionView: WmcRecognitionSingleOptionView = {
        let view = WmcRecognitionSingleOptionView()
        return view
    }()

    private lazy var line: UIView = {
        let view = UIView()
        view.backgroundColor = .wmc_grayH15_E6E6E6
        return view
    }()

    @objc
    private func switchDidChange(sender: UISwitch) {
        guard let indexPath = indexPath else { return }
        let data = (isOn: sender.isOn, indexPath: indexPath)
        switchClosure?(data)
    }

    @objc
    private func warehouseAction(sender: UIButton) {}
}
